package com.example.yichuangyun.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.DialogEditBinding;
import com.example.yichuangyun.utils.ToastHelper;

public class EditDialog {
    private Dialog dialog;
    private boolean isCancel = true;
    private DialogEditBinding binding;
    private String hintContent;

    public EditDialog(Activity activity) {
        if (dialog == null) {
            getDialog(activity);
        }
        hintContent = activity.getString(R.string.please_input_losing_order_reason);
    }

    public EditDialog(Activity activity, boolean isCancel) {
        this.isCancel = isCancel;
        if (dialog == null) {
            getDialog(activity);
        }
    }

    private void getDialog(Activity activity) {
        binding = DialogEditBinding.inflate(LayoutInflater.from(activity));
        dialog = new Dialog(activity, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(isCancel);
        Window dialogWindow = dialog.getWindow();
        // 去除遮罩效果
//        dialogWindow.setDimAmount(0f);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.main_menu_animStyle);
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(lp);
        binding.tvLeft.setOnClickListener(v -> {
            leftClick();
            dismissDialog();
        });
        binding.tvRight.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(binding.etContent.getText().toString().trim())) {
                rightClick(binding.etContent.getText().toString().trim());
                binding.etContent.setText("");
                dismissDialog();
            } else {
                ToastHelper.show(hintContent);
            }
        });
    }

    public void leftClick() {

    }

    public void rightClick(String content) {

    }

    public void setContent(String title, String hintContent) {
        binding.tvTitle.setText(title);
        binding.etContent.setHint(hintContent);
        this.hintContent = hintContent;
        binding.etContent.setText("");
    }

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
